package com.xe.icard;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.aphidmobile.flip.FlipViewController;
import com.xe.framework.XEActivity;
import com.xe.framework.XEApp;

public class Photos extends XEActivity {
	
	private FlipViewController flipView;
	private Photos mContext;

	private int[] ids = {
			R.drawable.p1, R.drawable.p2,
			R.drawable.p3, R.drawable.p4,
			R.drawable.p5, R.drawable.p6,
			R.drawable.p7, R.drawable.p8,
			R.drawable.p9, R.drawable.p10
	};
	private View op;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.photos);
        FrameLayout root = (FrameLayout) findViewById(R.id.photos);
        
        flipView = new FlipViewController(this);
		flipView.setAdapter(new BaseAdapter() {
			
			public int getCount() {
				return ids.length/2;
			}

			public Object getItem(int position) {
				return position;
			}

			public long getItemId(int position) {
				return position;
			}

			public View getView(int position, View convertView, ViewGroup parent) {
				if (convertView == null) {
					LayoutInflater inflater = LayoutInflater.from(mContext);
					convertView = inflater.inflate(R.layout.flipitem, null);
				}
				
				ImageView image1 = (ImageView) convertView.findViewById(R.id.photo1);
				ImageView image2 = (ImageView) convertView.findViewById(R.id.photo2);
				image1.setImageResource(ids[position*2]);
				image2.setImageResource(ids[position*2+1]);
				
				return convertView;
			}
		});

		root.addView(flipView, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		
		if(Config.IsShowOP()){
			root.addView(LayoutInflater.from(mContext).inflate(R.layout.opmask, null), new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			op = findViewById(R.id.op);
			op.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		flipView.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		flipView.onPause();
	}
	
	@Override
	public void onUserInteraction() {
		if(Config.IsShowOP()){
			Config.setShownOP();
			op = findViewById(R.id.op);
			op.setVisibility(View.GONE);
		}
		super.onUserInteraction();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if( KeyEvent.KEYCODE_BACK == keyCode ){
			XEApp.getInstance().getActivityManager().goBack(R.anim.in_from_left, R.anim.out_to_right);
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
